package com.ashafa.fileutils;

import java.util.*;
import java.io.*;

import org.apache.log4j.Logger;


public abstract class FileWatcher extends TimerTask {
  private static final Logger logger = Logger.getLogger(FileWatcher.class.getName()); 
  private long timeStamp;
  private File file;

  public FileWatcher( File file ) {
    this.file = file;
    this.timeStamp = file.lastModified();
  }

  public final void run() {
    long lastModified = file.lastModified();

    if( this.timeStamp != lastModified ) {
      this.timeStamp = lastModified;
      try{
    	  onChange(file);
       }catch(Exception e){
    	   logger.error(e);
      }
    }
  }

  protected abstract void onChange( File file ) throws Exception;
}